package action;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

import model.Notice;
import model.User;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.github.miemiedev.mybatis.paginator.domain.PageList;

import service.NoticeService;
import action.base.BaseAction;

@Scope("prototype")
@Controller
public class NoticeAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private NoticeService noticeService;
	private PageList<HashMap<String, Object>> pageList;
	private boolean success = false;
	private Notice notice;
	private int total;
	private HashMap<String, Object> oneNotice;

	public String saveOrUpdate() {
		User user = super.getUser();
		notice.setPublisher(user.getId());
		noticeService.saveOrUpdate(notice);
		success = true;
		return "success";
	}

	public String delete() {
		String ids = super.getParameterStr("ids");
		noticeService.delete(ids);
		success = true;
		return "success";
	}

	public String publish() {
		String ids = super.getParameterStr("ids");
		noticeService.publish(ids);
		success = true;
		return "success";
	}

	public String queryPage() throws UnsupportedEncodingException {
		int page = super.getParameterInt("page");
		int limit = super.getParameterInt("limit");
		User user = super.getUser();
		String title = super.getParameterStr("title");
		String beginDate = super.getParameterStr("beginDate");
		String endDate = super.getParameterStr("endDate");
		HashMap<String, Object> map = new HashMap<String, Object>();
		if (!StringUtils.isNotBlank(title)) {
			title = null;
		} else {
			byte[] _title = title.getBytes("ISO-8859-1");
			title = new String(_title, "UTF-8");
		}
		if (!StringUtils.isNotBlank(beginDate)) {
			beginDate = null;
		}
		if (!StringUtils.isNotBlank(endDate)) {
			endDate = null;
		}
		map.put("title", title);
		map.put("beginDate", beginDate);
		map.put("endDate", endDate);
		map.put("temp", user.getId());
		pageList = noticeService.queryPage(page, limit, map);
		setTotal(pageList.getPaginator().getTotalCount());
		return "queryPage";
	}

	public String queryPage4All() throws UnsupportedEncodingException {
		int page = super.getParameterInt("page");
		int limit = super.getParameterInt("limit");
		String title = super.getParameterStr("title");
		String beginDate = super.getParameterStr("beginDate");
		String endDate = super.getParameterStr("endDate");
		HashMap<String, Object> map = new HashMap<String, Object>();
		if (!StringUtils.isNotBlank(title)) {
			title = null;
		} else {
			byte[] _title = title.getBytes("ISO-8859-1");
			title = new String(_title, "UTF-8");
		}
		if (!StringUtils.isNotBlank(beginDate)) {
			beginDate = null;
		}
		if (!StringUtils.isNotBlank(endDate)) {
			endDate = null;
		}
		map.put("title", title);
		map.put("beginDate", beginDate);
		map.put("endDate", endDate);
		pageList = noticeService.queryPage(page, limit, map);
		setTotal(pageList.getPaginator().getTotalCount());
		return "queryPage";
	}

	public String getById() {
		int id = super.getParameterInt("id");
		notice = noticeService.getById(id);
		success = true;
		return "notice";

	}

	public String queryNotice() {
		int id = super.getParameterInt("id");
		oneNotice = noticeService.queryNotice(id);
		success = true;
		return "oneNotice";

	}

	public PageList<HashMap<String, Object>> getPageList() {
		return pageList;
	}

	public void setPageList(PageList<HashMap<String, Object>> pageList) {
		this.pageList = pageList;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public Notice getNotice() {
		return notice;
	}

	public void setNotice(Notice notice) {
		this.notice = notice;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public HashMap<String, Object> getOneNotice() {
		return oneNotice;
	}

	public void setOneNotice(HashMap<String, Object> oneNotice) {
		this.oneNotice = oneNotice;
	}

}
